/* File makes Future_dis.tex (Table A.7) and Future_rdis.tex (Table A.8); and CSV file to make Figure 8 */

/* 	Input files: 	fw_info_silo, stock_day,
	Output files: Future_dis.tex, Future_rdis.tex, Future_dis_mbinStockDayFE.csv, Future_rdis_mbinStockDayFE.csv  */

**********************************************
clear all
macro drop _all
scalar drive		= "E:"
scalar maindir		= "`=drive'\Replicate"
scalar tabledir		= "`=drive'\Replicate\TablesRep"
cd "`=maindir'"
**********************************************

use fw_info_silo, clear 
gduplicates drop
foreach var of varlist _all {
	rename `var' `=lower("`var'")'   // "
}

ren std_orig_signals s_disagree
ren std_rec_mean_signal r_disagree
ren mean_rec_signal_std r_uncertainty

drop if mi(r_disagree) | mi(s_disagree) | mi(r_uncertainty) 


merge m:1 date using mktdate_0719, keep(match) nogen 
tsset symbolid mktdate 
forvalues k=1/50 {
gen s_disagree_f`k'=f`k'.s_disagree
gen r_disagree_f`k'=f`k'.r_disagree
}
compress
save s_disagree_f, replace

use symbolid sdev_s_disagree avg_s_disagree sdev_r_disagree avg_r_disagree using stock_day, clear 
gduplicates drop

merge 1:m symbolid using s_disagree_f , keep(matched) nogen 

forvalues k=1/50 {
replace s_disagree_f`k'=(s_disagree_f`k'-avg_s_disagree)/sdev_s_disagree 
replace r_disagree_f`k'=(r_disagree_f`k'-avg_r_disagree)/sdev_r_disagree 
}
save s_disagree_f, replace


******************
use stock_day, clear

merge 1:1 date symbolid using s_disagree_f, keep(master match) nogen

drop ablogvol_f*
save temp_stock_day_dis, replace



use temp_stock_day_dis, clear
keep symbolid permno date std_orig_signals std_rec_mean_signal mean_rec_signal_std mbin* s_disagree_f* r_disagree_f*

scalar clvar = "permno date"

scalar define count= 0

foreach k in 1 10 30 50 {  
	scalar count = count +1
	eststo F`=count': reghdfe s_disagree_f`k' std_orig_signals std_rec_mean_signal mean_rec_signal_std  , noabsorb vce(cl `=clvar')    
		sum s_disagree_f`k' if e(sample)==1
		estadd scalar avg = r(mean) , : F`=count'
		cap estadd local mbinfe  "-"
		cap estadd local stockfe "-"
		cap estadd local dayfe   "-"
}

foreach k in 1 10 30 50  { 
	scalar count = count +1
	eststo F`=count': reghdfe s_disagree_f`k' std_orig_signals std_rec_mean_signal mean_rec_signal_std  , absorb(i.date i.permno i.mbin*) vce(cl `=clvar')    
		sum s_disagree_f`k' if e(sample)==1
		estadd scalar avg = r(mean) , : F`=count'
		cap estadd local mbinfe  "Y"
		cap estadd local stockfe "Y"
		cap estadd local dayfe   "Y"
}
			

			scalar define count= 0

			foreach k in 1 10 30 50 {   
			scalar count = count +1
			eststo R`=count': reghdfe r_disagree_f`k' std_orig_signals std_rec_mean_signal mean_rec_signal_std  , noabsorb vce(cl `=clvar')    
				sum r_disagree_f`k' if e(sample)==1
				estadd scalar avg = r(mean) , : R`=count'
				cap estadd local mbinfe  "-"
				cap estadd local stockfe "-"
				cap estadd local dayfe   "-"
			}

			foreach k in 1 10 30 50  {  
			scalar count = count +1
			eststo R`=count': reghdfe r_disagree_f`k' std_orig_signals std_rec_mean_signal mean_rec_signal_std  , absorb(i.date i.permno i.mbin*) vce(cl `=clvar')    
				sum r_disagree_f`k' if e(sample)==1
				estadd scalar avg = r(mean) , : R`=count'
				cap estadd local mbinfe  "Y"
				cap estadd local stockfe "Y"
				cap estadd local dayfe   "Y"
			}


* Daily estimate (for Figure 8)
forval k = 1/50 {   
	scalar count = count +1
		
	eststo D`=count': reghdfe s_disagree_f`k' std_orig_signals std_rec_mean_signal mean_rec_signal_std  , absorb(i.date i.permno i.mbin*) vce(cl `=clvar')  
		sum s_disagree_f`k' if e(sample)==1
		estadd scalar avg = r(mean) , : D`=count'
		cap estadd local mbinfe  "Y"
		cap estadd local stockfe "Y"
		cap estadd local dayfe   "Y"	
}


forval k = 1/50 {   
	scalar count = count +1
		
	eststo X`=count': reghdfe r_disagree_f`k' std_orig_signals std_rec_mean_signal mean_rec_signal_std  , absorb(i.date i.permno i.mbin*) vce(cl `=clvar')   
		sum r_disagree_f`k' if e(sample)==1
		estadd scalar avg = r(mean) , : X`=count'
		cap estadd local mbinfe  "Y"
		cap estadd local stockfe "Y"
		cap estadd local dayfe   "Y"	
}


* ---------------------
local stats_opts " stats(N N_clust1 N_clust2 r2 avg mbinfe stockfe dayfe, fmt(%18.0fc %18.0fc %18.0fc %12.2fc %12.2fc) labels("\# obs." "\# clusters (stock)" "\# clusters (day)" "$\textit{R}^2$" "Mean of dependent var." "Message number FE" "Stock FE" "Day FE") ) " 

#delimit ; 
local opts "  b(%16.3fc) se(%16.3fc) brackets booktabs  star(* 0.10 ** 0.05 *** 0.01) nonotes lines 
mtitles("t+1" "t+10" "t+30" "t+50" "t+1" "t+10" "t+30" "t+50"  )
mgroups("Dep. var.: Sender Disagreement\textsubscript{\textbf{s,t+x}}"
, pattern(1 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  " ;        

cd "`=tabledir'" ;

esttab F1 F2 F3 F4 F5 F6 F7 F8 using Future_dis.tex,  `opts' `stats_opts'  drop(_cons) order(std_orig_signals std_rec_mean_signal mean_rec_signal_std) coef(
std_orig_signals "Sender Disagreement\textsubscript{s,t}" 
std_rec_mean_signal "Received Disagreement\textsubscript{s,t}" 
mean_rec_signal_std "Received Uncertainty\textsubscript{s,t}" 
) replace  ;



#delimit ; 
local opts "  b(%16.3fc) se(%16.3fc) brackets booktabs  star(* 0.10 ** 0.05 *** 0.01) nonotes lines 
mtitles("t+1" "t+10" "t+30" "t+50" "t+1" "t+10" "t+30" "t+50"  )
mgroups("Dep. var.: Received Disagreement\textsubscript{\textbf{s,t+x}}"
, pattern(1 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  " ;      

esttab R1 R2 R3 R4 R5 R6 R7 R8 using Future_rdis.tex,  `opts' `stats_opts'  drop(_cons) order(std_orig_signals std_rec_mean_signal mean_rec_signal_std) coef(
std_orig_signals "Sender Disagreement\textsubscript{s,t}" 
std_rec_mean_signal "Received Disagreement\textsubscript{s,t}" 
mean_rec_signal_std "Received Uncertainty\textsubscript{s,t}" 
) replace  ;



local opts "  b(%16.3fc) se(%16.3fc) brackets booktabs  star(* 0.10 ** 0.05 *** 0.01) nonotes lines 
mtitles("t+1" "t+2" "t+3" "t+4" "t+5" "etc" )
mgroups("Dep. var.: Sender Disagreement\textsubscript{\textbf{s,t+x}}"
, pattern(1 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  " ;       

esttab D* using Future_dis_mbinStockDayFE.csv,  `opts' `stats_opts' replace ;


local opts "  b(%16.3fc) se(%16.3fc) brackets booktabs  star(* 0.10 ** 0.05 *** 0.01) nonotes lines 
mtitles("t+1" "t+2" "t+3" "t+4" "t+5" "etc" )
mgroups("Dep. var.: Received Disagreement\textsubscript{\textbf{s,t+x}}"
, pattern(1 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  " ;       
esttab X* using Future_rdis_mbinStockDayFE.csv, `opts' `stats_opts' replace ;

#delimit cr 
cd "`=maindir'"




cap n erase temp_stock_day_dis.dta




